<?php
namespace app\home\controller;
use app\common\controller\Home;
use think\Cache;
class Api extends Home {
    public function index()
	{
		return 'api';
    }
    
    public function read() {
		$param = zanpiancms_param_url();
        $array_detail = $this->get_cache_detail($param);
		if($array_detail){			
		    return json($array_detail);
		} else {
            return json(['data' => []]);
        }
	}
	
	public function play(){
		$param = zanpiancms_param_url();
        $array_detail = $this->get_cache_detail($param);
		if($array_detail){
			$array_detail=$this->Lable_Vod_Play($param,$array_detail);		
		    return json($array_detail);
		}
		return json(['data' => []]);
    }

    public function list() {
        $tags = zanpiancms_param_url();
        $param = array('page' => true, 'limit' => 30);
        $list = model('Vod') -> listData($param);
        print_r($list);
        return json($list);
    }

    //从数据库获取数据
	private function get_cache_detail($param){
		if(empty($param['id']) && empty($param['pinyin'])){ return false;}
		//优先读取缓存数据
		if($GLOBALS['config']['cache']['data_cache_vod']){
			$array_detail = Cache::get('data_cache_vod_'.$param['id'].$param['pinyin']);
			if($array_detail){
				return $array_detail;
			}
		}
		//未中缓存则从数据库读取
		$where = array();
		if(!empty($param['id'])){
		    $where['vod_id'] = $param['id'];
		}
		if(!empty($param['pinyin'])){
		    $where['vod_letters'] = $param['pinyin'];
		}		
		$where['vod_status'] = array('eq',1);
		$data = db('vod')->alias('vod')->join('actor actor','actor.actor_vid =vod.vod_id','LEFT')->join('story story','story.story_vid =vod.vod_id','LEFT')->where($where)->find();
		if(!empty($data)){
			//解析标签
			$array_detail = $this->Lable_Vod_Read($data,1);
			if($GLOBALS['config']['cache']['data_cache_vod']){
				Cache::tag('data_vod')->set('data_cache_vod_'.$param['id'].$param['pinyin'],$array_detail,intval($GLOBALS['config']['cache']['data_cache_vod'])); 
			}
			return $array_detail;
		}
		return false;
	}
}